package com.zlzvip.android.app;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.pachong.android.baseuicomponent.activity.BaseActivity;
import com.zlzvip.android.common.web.CommonWebviewFragment;

import java.util.Timer;
import java.util.TimerTask;


public class MainActivity extends BaseActivity {
    private CommonWebviewFragment mWebviewFragment;
    private static boolean isExit = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        hideToolbar();
//        String mUrl = "http://baidu.com";
        String mUrl = "http://h5.zlzvip.com";
        mWebviewFragment = CommonWebviewFragment.newInstance(mUrl);
        getSupportFragmentManager().beginTransaction().add(com.pachong.android.framework.R.id.flytWebview, mWebviewFragment).commit();
     /*   TokenInfo info = new TokenInfo();
        info.setExpiresAt("");
        info.setAccessToken("AF318BE799982C97457092AEDA03CE4818724A13D80CF940E75D8AD2E24FB88ABD3165327F97AABB");
        info.setRefreshToken("AF318BE799982C97AECD790497EA6F4CD76F46F2ADECB9A9562CBD088F3E8D02E7A2A6D1E1419C0D");
        info.setServerTime("2016-07-21T14:43:41.080+0800");
        info.setMacKey("qjBDKRD6yJ");

        String url = "http://120.76.200.15:8888/uc/user";
        String authStr = Authorization.createAuthorization(info, url, "get");
        Log.e("vivi", "  auth string === " + authStr);*/

//        testGetAppAttrs();

//        String path = FileManager.getWebCachePath(this) + "zygote_web_cache.zip";
//        String toPath = FileManager.getWebCachePath(this) + "zygote_web_cache";
//        FileUtil.copyAssetToSDCard(getAssets(), "www/zygote_web_cache.zip", path);
//
//
//        ZipExtractor zipExtractor = new ZipExtractor(this,
//                path, toPath,
//                new ZipExtractor.OnExtractListener() {
//                    @Override
//                    public void onProgressUpdate(long currSize) {
//                        SLog.e("onProgressUpdate currSize == " + currSize);
//                    }
//                    @Override
//                    public void onComplete(long totalSize) {
//                        SLog.e("onComplete totalSize == " + totalSize);
//                    }
//
//                    @Override
//                    public void onFailure(int errorCode, long totalSize,
//                                          long currSize) {
////                        SLog.e("onFailure errorCode == " + errorCode + "totalSize" + totalSize);
//                    }
//
//                    @Override
//                    public void onCaculateTotalSize(long totalSize) {
//                        SLog.e("onCaculateTotalSize totalSize == " + totalSize);
//                    }
//
//                });
//        zipExtractor.execute();


    }

    @Override
    public View createDataView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
        return layoutInflater.inflate(com.pachong.android.framework.R.layout.activity_common_webview, null);
    }


    public static void start(Activity activity){
        Intent intent = new Intent(activity, MainActivity.class);
        activity.startActivity(intent);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK &&mWebviewFragment.getWebview().canGoBack()) {
            mWebviewFragment.getWebview().goBack();
        }else{
            if(!isExit) {
                isExit = true;
                Toast.makeText(this, "在按一次退出程序", Toast.LENGTH_SHORT).show();
                new Timer().schedule(new TimerTask() {
                    @Override
                    public void run() {
                        isExit = false;
                    }
                }, 2000);
            } else {
                Intent startMain = new Intent(Intent.ACTION_MAIN);
                startMain.addCategory(Intent.CATEGORY_HOME);
                startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(startMain);
                System.exit(0);//退出程序
//                           finish();
            }
        }
        return false;
    }

}
